<?php

/**
 * 公司管理
 * 公司的状态state字段
 * 1：正常状态
 */
if (!defined('IN_DIALOG'))
    die("Access Denied");

$op = in_array($_GET['op'], array('list', 'add', 'search', 'edit')) ? $_GET['op'] : '';

$_SGLOBAL['view']->assign('ac', $ac);
$_SGLOBAL['view']->assign('op', $op);

$list = array();

if ($op == 'add') {
    //添加公司
    if (Utils::submitCheck('addsubmit')) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $website = $_POST['website'];
        $province = $_POST['province'];
        $city = $_POST['city'];

        $setarr = array('name' => $name, 'email' => $email,
            'website' => $website,
            'province' => $province,
            'city' => $city, 'dateline' => $_SGLOBAL['timestamp']);
        if ($_SGLOBAL['db']->insertTable('company', $setarr)) {
            Utils::showMessage('do_success', $_SGLOBAL['refer']);
        } else {
            Utils::showMessage('do_error', $_SGLOBAL['refer']);
        }
    }
    $_SGLOBAL['view']->display('setup_company_add');
} elseif ($op == 'list') {
    //公司列表
    $query = $_SGLOBAL['db']->query("SELECT * FROM " . Utils::tname('company') . " LIMIT 0," . $_SCONFIG['pagesize']);
    while ($row = $_SGLOBAL['db']->fetchArray($query)) {
        $row['dateline'] = Utils::sDate($row['dateline'], 'Y-m-d H:i:s');
        $list[] = $row;
    }
    $_SGLOBAL['view']->assign('companylist', $list);
    $_SGLOBAL['view']->display('setup_company_list');
} elseif ($op == 'search') {
    //搜索
    if (Utils::submitCheck('searchsubmit')) {
        $name = trim($_GET['name']);
        $query = $_SGLOBAL['db']->query("SELECT * FROM " . Utils::tname('company') . " WHERE name='$name'");
        while ($row = $_SGLOBAL['db']->fetchArray($query)) {
            $row['dateline'] = Utils::sDate($row['dateline'], 'Y-m-d H:i:s');
            $list[] = $row;
        }
        $_SGLOBAL['view']->assign('name', $name);
    }
    $_SGLOBAL['view']->assign('companylist', $list);
    $_SGLOBAL['view']->display('setup_company_search');
} elseif ($op == 'edit') {
    //编辑公司
    $cid = (int) $_GET['cid'];
    if (!$cid) {
        Utils::showMessage('company_not_exists');
    }
    $query = $_SGLOBAL['db']->query("SELECT * FROM " . Utils::tname('company') . " WHERE cid=$cid");
    if (!$query) {
        Utils::showMessage('company_not_exists');
    }
    if (Utils::submitCheck('editsubmit')) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $website = $_POST['website'];
        $province = $_POST['province'];
        $city = $_POST['city'];

        $setarr = array('name' => $name, 'email' => $email,
            'website' => $website, 'province' => $province, 'city' => $city);
        if ($_SGLOBAL['db']->updateTable('company', $setarr, array('cid' => $cid))) {
            Utils::showMessage('do_success');
        } else {
            Utils::showMessage('do_error');
        }
    }

    $company = $_SGLOBAL['db']->fetchArray($query);

    $_SGLOBAL['view']->assign('company', $company);
    $_SGLOBAL['view']->display('setup_company_edit');
} else {
    $_SGLOBAL['view']->assign('op', $op);
    $_SGLOBAL['view']->display('setup_company');
}
?>
